package com.example.touch.domain;

import java.util.Collection;

import android.app.Activity;

/**
*
* @author pla
*/
public class SequentialActionInvoker extends AbstractActionSequence
{
	private static final long serialVersionUID = 5611251909098829152L;
	private long interval;
	private Activity currentActivity;
	
	public SequentialActionInvoker(Activity currentActivity, Collection<IActivityAction> activityActions, int intervalSeconds) 
	{
		super(activityActions);
		this.currentActivity = currentActivity;
		interval = intervalSeconds * 1000;
	}

	// Probably doesn't work :-)
	@Override
	protected void doExecuteAll(final Collection<IActivityAction> activityActions) 
	{
		Thread workerThread = new Thread(new Runnable() 
			{
				@Override
				public void run() 
				{
					doWork(activityActions);
				}
				
			});
		
		workerThread.start();
	}
	
	private void doWork(Collection<IActivityAction> activityActions)
	{
		for(final IActivityAction action : activityActions)
		{
			currentActivity.runOnUiThread(new Runnable()
			{
				@Override
				public void run() 
				{
					action.invoke();
				}
			});
			
			try 
			{
				Thread.sleep(interval);
			} 
			catch (InterruptedException e) 
			{
				e.printStackTrace();
			}
		}
	}
}
